/**
 * 计算一年的天数
 */
function NumDates(t) {//t 代表指定的参数 2019
  if (t == null) {
    var Year = new Date().getFullYear(), s = 0, d;//获取当前年
    for (var i = 1; i < 13; i++) {
      d = new Date(Year, i, 0);//获取某一个月的天数
      s += d.getDate();
    };
    return s;
  } else if (t >= 1970) {
    var Year = new Date().getFullYear(), s = 0, d;
    for (var i = 1; i < 13; i++) {
      d = new Date(t, i, 0);
      s += d.getDate();
    };
    return s;
  } else {
    return "年份有误";
  }
}

/**
 * 计算一年中的周数
 */
function getweekNums(year) {  //year = 2018
  let d2 = new Date(year + "-01-01");
  // console.log("d2",d2)
  // d2.setMonth(0);
  // d2.setDate(1);
  // console.log("d22",d2)
  let offset = d2.getDay();
  if (offset == 0) {
    offset = 7;
  }
  return Math.floor((parseInt(NumDates(year)) + offset - 1) / 7);
}

/**
 * 判断周数
 */
function getWeekNum(dt) {
  let d1 = new Date(dt);
  let d2 = new Date(dt);
  d2.setMonth(0);
  d2.setDate(1);
  let offset = d2.getDay();
  if (offset == 0) {
    offset = 7;
  }
  let weekNum = getweekNums(d1.getFullYear());
  let rq = d1 - d2;
  let days = Math.floor(rq / (24 * 60 * 60 * 1000));
  let num = Math.floor((days + offset - 1) / 7) + 1;
  return String(num);
  // console.log("看一看", days + offset - 1)
  // if ((days + offset - 1) % 7 == 0) {
  //     console.log("刚刚好整年:", num)
  //     return String(num);
  // } else {
  //     if (num == weekNum + 1) {
  //         num = 1;
  //     }
  //     console.log("刚刚好整年2:", num)
  //     return String(num);
  // }
}

/**
 * 获取一个月的天数
 */
function getMonthDaysNum(year, month) {
  var d = new Date(year, month, 0);
  return d.getDate();
}

/**
  * 判断周几
  */
function getDayName(day) {
  var weekDay = [
      "星期天",
      "星期一",
      "星期二",
      "星期三",
      "星期四",
      "星期五",
      "星期六"
  ];
  var date = new Date(Date.parse(day));
  return weekDay[date.getDay()];
}

/**
 * 获取本周一，date格式 2019-11-21
 */
function getMonDate(date) {
  var today = new Date(date);
  // console.log(today)
  var now = today.getTime();
  var jour = today.getDay() == 0 ? 7:today.getDay();
  var offset = ((((jour-1)*24 + today.getHours())*60 + today.getMinutes())*60 + today.getSeconds())*1000 +today.getMilliseconds();
  var monday = new Date(now - offset);
  monday = monday.getFullYear()+'-'+ (parseInt(monday.getMonth())+1)+'-'+monday.getDate();
  return monday;
}

/**
 * 求和
 */
function getSum(arr){
  var sum = 0;
  for(var i in arr){
    sum += arr[i];
  }
  return sum;
}
export default { NumDates, getweekNums, getWeekNum, getMonthDaysNum, getDayName, getMonDate, getSum};



